package com.bdqn;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Hello world!
 */
public class ShortAnswer1And3_3 {

    public static void main(String[] args) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse("src/main/java/com/bdqn/Student.xml");

            // 创建新节点student
            Element student = document.createElement("student");
            student.setAttribute("id","3");

            // 创建student的子节点name、course、score
            Element name = document.createElement("name");
            Element course = document.createElement("course");
            Element score = document.createElement("score");

            // name、course、score的文本值
            name.appendChild(document.createTextNode("李白"));
            course.appendChild(document.createTextNode("数学"));
            score.appendChild(document.createTextNode("60"));

            // 把子元素放进父元素中
            // 第一个学生的数据
            student.appendChild(document.createTextNode("\n\t"));
            student.appendChild(name);
            student.appendChild(document.createTextNode("\n\t"));
            student.appendChild(course);
            student.appendChild(document.createTextNode("\n\t"));
            student.appendChild(score);
            student.appendChild(document.createTextNode("\n"));

            // 找到scores元素的位置，把所有元素数据放进scores元素标签中
            Node scores = document.getElementsByTagName("scores").item(0);
            scores.appendChild(document.createTextNode("\n"));
            scores.appendChild(student);
            scores.appendChild(document.createTextNode("\n\n"));

            // 保存XML文件
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource domSource = new DOMSource(document);
            // 设置编码类型

            transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
            StreamResult result = new StreamResult(new FileOutputStream("src/main/java/com/bdqn/Student.xml"));
            // 把DOM树转换为XML文件

            transformer.transform(domSource,result);
            System.out.println("添加成功");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }


    }

}
